package com.liuxingzhiwu.ruijishangcheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuxingzhiwu.ruijishangcheng.common.R;
import com.liuxingzhiwu.ruijishangcheng.entity.Category;
import com.liuxingzhiwu.ruijishangcheng.service.CategoryService;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询餐品分类
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        //分页查询
        Page<Category> pageInfo = new Page<>(page,pageSize);
        Page<Category> pageCategory = categoryService.page(pageInfo,queryWrapper);

        return R.success(pageCategory);
    }

    /**
     * 添加餐品分类
     * @param request
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Category category){

        //添加创建时间
        category.setCreateTime(LocalDateTime.now());

        //添加更新使劲按
        category.setUpdateTime(LocalDateTime.now());

        //添加创建者id
        Long empId = (Long) request.getSession().getAttribute("employee");
        category.setCreateUser(empId);

        //添加更新者id
        category.setUpdateUser(empId);

        //添加进数据库
        Boolean ret = categoryService.save(category);

        if(ret){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    /**
     * 更新餐品分类
     * @param request
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateCategory(HttpServletRequest request,@RequestBody Category category){

        //修改时间
        category.setUpdateTime(LocalDateTime.now());

        //修改者id
        Long empId = (Long) request.getSession().getAttribute("employee");
        category.setUpdateUser(empId);

        //更新数据库
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Category::getId,category.getId());
        Boolean ret = categoryService.update(category,queryWrapper);

        if(ret){
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }

    /**
     * 删除餐品分类
     * @param request
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(HttpServletRequest request,Long ids){
        Category category = new Category();

        category.setId(ids);

        //修改（删除）时间
        category.setUpdateTime(LocalDateTime.now());

        //修改（删除）者id
        Long empId = (Long)  request.getSession().getAttribute("employee");
        category.setUpdateUser(empId);

        Boolean ret = categoryService.removeById(category);

        if(ret){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    @GetMapping("/list")
    public R<List> getCategory(Integer type){

        //获取id和name连个字段
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        //获取类型为1，也就是菜品分类，不是套餐
        queryWrapper.eq(ObjectUtils.isNotEmpty(type),Category::getType,type);
        List list = categoryService.list(queryWrapper);
        return R.success(list);
    }



}
